package com.yixun.qfbao.hander;

import java.util.List;

import com.yixun.qfbao.apidto.AddReportCustomerBean;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.AddReportService;
import com.yixun.qfbao.api.service.PremisesRecommenderService;
import com.yixun.qfbao.apidto.AddReportReqDto;
import com.yixun.qfbao.apidto.AddReportResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.ReportModel;
import com.yixun.qfbao.model.dto.ReportModelDto;
import com.yixun.qfbao.model.enums.DefaultReportModelEnum;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.ModelTypeEnum;
import com.yixun.qfbao.model.enums.ReportModelShowStatusEnum;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.service.ReportModelService;
import com.yixun.qfbao.util.WxAccessTokenCacheUtils;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.vo.AddReportReqVo;
import com.yixun.qfbao.xf.model.PremisesRecommender;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * 新增报备
 * @author JHSong
 *
 */
@Service("addReportHander")
public class AddReportHander extends CommonAbstractHander {
	private static Logger logger= LoggerFactory.getLogger(AddReportHander.class);
	
	@Autowired
	private AddReportService addReportService;
	@Autowired
    private WxAccessTokenCacheUtils wxAccessTokenCacheUtils;
	@Autowired
	private ReportModelService reportModelService;
	@Autowired
	private PremisesRecommenderService premisesRecommenderService;
	
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		AddReportReqVo req = (AddReportReqVo)reqDto;
		AddReportReqDto reqDto1 = req.getParams();
		BaseResponseDTO<AddReportResDto> res = new BaseResponseDTO<AddReportResDto>();
		AddReportResDto resDto = new AddReportResDto();
		// 报备推荐为空，增加校验
		if(StringUtils.isEmpty(reqDto1.getRecommenderUuid())){
			resDto = checkModelShowStatus(reqDto1);
			if(resDto != null){
				res.setResData(resDto);
				res.setResult(resDto.getResult());
				return res;
			}
		}

		try {
            //获取案场助手的微信accessToken
            String assistantAccessToken = wxAccessTokenCacheUtils.getAccessToken(reqDto1.getCompanyUuid(), SystemIdEnum.ASSISTANT.getCode(), reqDto1.getDomain());
			resDto = addReportService.addReport(reqDto1, assistantAccessToken);
			res.setResData(resDto);
			res.setResult(resDto.getResult());
		}catch(TServerException e){
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg(e.getErrorMsg());
		} catch (Exception e) {
			logger.error("AddReportHander error: {}", e);
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg("系统异常");
		}
		return res;
	}

	// 检查模板展示状态
	private AddReportResDto checkModelShowStatus(AddReportReqDto reqDto){
		AddReportResDto resDto = new AddReportResDto();
		// 查询所有预制的报备模板
		ReportModelDto reportModelDto = new ReportModelDto();
		reportModelDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		reportModelDto.setPremisesId(Long.parseLong(reqDto.getPremisesId()));
		reportModelDto.setModelType(ModelTypeEnum.base_config.getCode());
		List<ReportModel> list = reportModelService.selectListByDto(reportModelDto);
		ReportModel model = new ReportModel();
		for(ReportModel rm : list){
			if(DefaultReportModelEnum.RECOMMENDER.getModelColumn().equals(rm.getModelColumn())){
				model = rm;
			}
		}
		List<PremisesRecommender> recommenders = premisesRecommenderService.selectPremisesRecommenderByPremisesId(Long.parseLong(reqDto.getPremisesId()));
		if(ReportModelShowStatusEnum.required.getCode().equals(model.getShowFlag()) && CollectionUtils.isNotEmpty(recommenders)){
			logger.error("Recommender is null");
			resDto.setResult(ResultEnum.FAIL.getNo());
			resDto.setResultMsg("请填写报备推荐人");
			return resDto;
		}
		return null;
	}

}
